package Server.Poker.TTZ;

import java.util.Random;


//28��
public class GameLogic {

	final int m_cbCardListData[] = { 
			//1-9Ͳ     4���װ�
			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
			
	};

	public int CountArray(int by[]) {
		return by.length;
	}

	public int sizeof(int by[]) {
		return by.length;
	}

	public void CopyMemory(int[] by, int[] by2, int length) {
		for (int i = 0; i < length; i++) {
			by[i] = by2[i];
		}
	}

	public Random rd = new Random();

	public int rand() {
		return rd.nextInt(1000);
	}

	// �����˿�
	void RandCardList(int cbCardBuffer[], int cbBufferCount) {
		// ����׼��
		int cbCardData[] = new int[CountArray(m_cbCardListData)];
		CopyMemory(cbCardData, m_cbCardListData, sizeof(m_cbCardListData));

		// �����˿�
		int cbRandCount = 0;
		int cbPosition = 0;
		do {
			cbPosition = rand() % (CountArray(cbCardData) - cbRandCount);
			cbCardBuffer[cbRandCount++] = cbCardData[cbPosition];
			cbCardData[cbPosition] = cbCardData[CountArray(cbCardData) - cbRandCount];
		} while (cbRandCount < cbBufferCount);

		return;
	}

	void RandCardListEx(int cbCardBuffer[], int cbBufferCount) {
		int cbCardData[] = new int[cbBufferCount];
		CopyMemory(cbCardData, cbCardBuffer, cbBufferCount);

		// �����˿�
		int cbRandCount = 0;
		int cbPosition = 0;
		do {
			cbPosition = rand() % (cbBufferCount - cbRandCount);
			cbCardBuffer[cbRandCount++] = cbCardData[cbPosition];
			cbCardData[cbPosition] = cbCardData[cbBufferCount - cbRandCount];
		} while (cbRandCount < cbBufferCount);
	}

	//1-9�� ��һ��28��
	final int CT_SPECIAL_0 = 0;
	final int CT_SPECIAL_1 = 1;
	final int CT_SPECIAL_2 = 2;
	final int CT_SPECIAL_3 = 3;
	final int CT_SPECIAL_4 = 4;
	final int CT_SPECIAL_5 = 5;
	final int CT_SPECIAL_6 = 6;
	final int CT_SPECIAL_7 = 7;
	final int CT_SPECIAL_8 = 8;
	final int CT_SPECIAL_9 = 9;
	final int CT_SPECIAL_28 = 10;
	
	//����
	final int CT_SPECIAL_11 = 11;
	final int CT_SPECIAL_22 = 12;
	final int CT_SPECIAL_33 = 13;
	final int CT_SPECIAL_44 = 14;
	final int CT_SPECIAL_55 = 15;
	final int CT_SPECIAL_66 = 16;
	final int CT_SPECIAL_77 = 17;
	final int CT_SPECIAL_88 = 18;
	final int CT_SPECIAL_99 = 19;
	final int CT_SPECIAL_1010 = 20;
	final int CT_POINT = 0;

	int RetType(int itype) {
		itype = itype % 10;
		switch (itype) {
		case 0:
			return CT_SPECIAL_0;
		case 1:
			return CT_SPECIAL_1;
		case 2:
			return CT_SPECIAL_2;
		case 3:
			return CT_SPECIAL_3;
		case 4:
			return CT_SPECIAL_4;
		case 5:
			return CT_SPECIAL_5;
		case 6:
			return CT_SPECIAL_6;
		case 7:
			return CT_SPECIAL_7;
		case 8:
			return CT_SPECIAL_8;
		case 9:
			return CT_SPECIAL_9;
		default:
			return CT_POINT;
		}
	}

	public int CT_ERROR_CARD = 0;
	public int NIUNIU_CARD_COUNT = 40;
	public int ST_VALUE = 1; // ��ֵ����
	public int ST_NEW = 3; // ��ֵ����
	public int ST_LOGIC = 2; // �߼�����
	
	// ��ȡ����
	int GetCardType(int cbCardData[], int cbCardCount) {
		return GetCardType(cbCardData, cbCardCount, null);
	}
	String GetCardStringType(int type){
		switch(type){
			case 12:
				return "双天至尊";
			case 11:
				return "豹子";
			case 10:
				return "二八杠";
		}
		return "";
	}
	int GetCardType(int cbCardData[], int cbCardCount, int[] bcOutCadData) {
		int cbCardDataSort[] = new int[NIUNIU_CARD_COUNT];
		CopyMemory(cbCardDataSort, cbCardData, cbCardCount);
		SortCardList(cbCardDataSort, cbCardCount, ST_VALUE);
		if (bcOutCadData != null) {
			CopyMemory(bcOutCadData, cbCardDataSort, cbCardCount);
		}

		int cardValue0 = GetCardValue(cbCardDataSort[0]);
		int cardValue1 = GetCardValue(cbCardDataSort[1]);

		boolean erYiYang = false;// ����ֵһ��
		if (cardValue0 == cardValue1) {
			erYiYang = true;
		}
		
		// ��ͬ�İװ�
		if (erYiYang && cardValue0 == 10) {
			return CT_SPECIAL_1010; //20
		}
		
		// �챪
		if (erYiYang && cardValue0 == 9) {
			return CT_SPECIAL_99; //19
		}
		
		//����
		if (erYiYang) {
			switch (cardValue0) {
			case 1:
				return CT_SPECIAL_11;
			case 2:
				return CT_SPECIAL_22;
			case 3:
				return CT_SPECIAL_33;
			case 4:
				return CT_SPECIAL_44;
			case 5:
				return CT_SPECIAL_55;
			case 6:
				return CT_SPECIAL_66;
			case 7:
				return CT_SPECIAL_77;
			case 8:
				return CT_SPECIAL_88;
			}
		}

		if(TTZTable.m_28gang!=0){
			//���˸�
			if((cardValue0+cardValue1)==10){
				if(cardValue0==2 && cardValue1==8){
					return CT_SPECIAL_28;
				}
				if(cardValue0==8 && cardValue1==2){
					return CT_SPECIAL_28;
				}
			}
		}else{
			if((cardValue0+cardValue1)==10){
				return CT_SPECIAL_0;
			}
		}
		
		int sum = 0;
		for (int i = 0; i < cbCardDataSort.length; i++) {
			int tmpCardData = cbCardDataSort[i];
			int cbCardValue = GetCardValue(tmpCardData);			
			if (cbCardValue >= 10) {
			} else {
				sum += cbCardValue;
			}
		}
		// 0~9
		sum = sum % 10;
		switch (sum) {
		case 0:
			return CT_SPECIAL_0;
		case 1:
			return CT_SPECIAL_1;
		case 2:
			return CT_SPECIAL_2;
		case 3:
			return CT_SPECIAL_3;
		case 4:
			return CT_SPECIAL_4;
		case 5:
			return CT_SPECIAL_5;
		case 6:
			return CT_SPECIAL_6;
		case 7:
			return CT_SPECIAL_7;
		case 8:
			return CT_SPECIAL_8;
		case 9:
			return CT_SPECIAL_9;
		default:
			return CT_POINT;
		}
	}

	// ��С�Ƚ�
	/*
	 * cbNextCardData>cbFirstCardData ����1 cbNextCardData<cbFirstCardData ����-1
	 * cbNextCardData==cbFirstCardData ����0
	 */
	// Multiple �Ƚϳ����ı���
	int CompareCard(int cbFirstCardData[], int cbNextCardData[]) {
		if (cbFirstCardData.length != cbNextCardData.length) {
			return 0;
		}
		for (int i = 0; i < cbFirstCardData.length; i++) {

			int firstValue = GetCardNewValue(cbFirstCardData[0]);
			int NextValue = GetCardNewValue(cbNextCardData[0]);

			if (firstValue != NextValue) {
				return NextValue > firstValue ? 1 : -1;
			}
		}
		return 0;
	}

	int CompareCard(int cbFirstCardData[], int cbFirstCardCount, int cbNextCardData[], int cbNextCardCount) {
		// �Ϸ��ж�
		if (!(2 == cbFirstCardCount && 2 == cbNextCardCount)){
			return 0;
		}
		// ��ȡ����
		int cbFirstCardType = GetCardType(cbFirstCardData, cbFirstCardCount);
		int cbNextCardType = GetCardType(cbNextCardData, cbNextCardCount);
		
		// ���ͱȽ�
		if (cbFirstCardType != cbNextCardType) {
			return cbNextCardType > cbFirstCardType ? 1 : -1;
		} else {
			// �����˿�
			int cbFirstCardDataTmp[] = new int[NIUNIU_CARD_COUNT];
			int cbNextCardDataTmp[] = new int[NIUNIU_CARD_COUNT];

			CopyMemory(cbFirstCardDataTmp, cbFirstCardData, cbFirstCardCount);
			CopyMemory(cbNextCardDataTmp, cbNextCardData, cbNextCardCount);
			SortCardList(cbFirstCardDataTmp, cbFirstCardCount, ST_VALUE);
			SortCardList(cbNextCardDataTmp, cbNextCardCount, ST_VALUE);
			
			return CompareCard(cbFirstCardDataTmp, cbNextCardDataTmp);
		}
	}

	// ��ȡ�Ƶ�
	/*int GetCardListPip(int cbCardData[], int cbCardCount) {
		// ��������
		int cbPipCount = 0;

		// ��ȡ�Ƶ�
		int cbCardValue = 0;
		for (int i = 0; i < cbCardCount; i++) {
			cbCardValue = GetCardValue(cbCardData[i]);
			if (cbCardValue > 10) {
				cbCardValue = 10;

			}
			cbPipCount += cbCardValue;
		}
		return (int) (cbPipCount % 10);
	}*/

	int LOGIC_MASK_VALUE = 0x0F; // ��ֵ����
	
	// ��ȡ��ֵ;
	int GetCardValue(int cbCardData) {
		return (int) (cbCardData & LOGIC_MASK_VALUE);
	}


	int GetCardNewValue(int cbCardData) {
		// �齫��������
		int cbCardValue = GetCardValue(cbCardData);

		return cbCardValue;
	}

	// �߼���С
	int GetCardLogicValue(int cbCardData) {
		int cbValue = GetCardValue(cbCardData);
		if (cbValue > 10) {
			cbValue = 10;
		}
		return cbValue;
	}

	// �����˿�
	void SortCardList(int cbCardData[], int cbCardCount, int cbSortType) {
		// ��Ŀ����
		if (cbCardCount == 0)
			return;

		// ת����ֵ
		int cbSortValue[] = new int[NIUNIU_CARD_COUNT];
		if (ST_VALUE == cbSortType) {
			for (int i = 0; i < cbCardCount; i++)
				cbSortValue[i] = GetCardValue(cbCardData[i]);
		} else {
			if (cbSortType == ST_NEW) {
				for (int i = 0; i < cbCardCount; i++)
					cbSortValue[i] = GetCardNewValue(cbCardData[i]);
			} else {
				for (int i = 0; i < cbCardCount; i++)
					cbSortValue[i] = GetCardLogicValue(cbCardData[i]);
			}
		}

		// �������
		boolean bSorted = true;
		int cbThreeCount;
		int cbLast = (int) (cbCardCount - 1);
		do {
			bSorted = true;
			for (int i = 0; i < cbLast; i++) {
				if ((cbSortValue[i] < cbSortValue[i + 1])
						|| ((cbSortValue[i] == cbSortValue[i + 1]) && (cbCardData[i] < cbCardData[i + 1]))) {
					// ����λ��
					cbThreeCount = cbCardData[i];
					cbCardData[i] = cbCardData[i + 1];
					cbCardData[i + 1] = cbThreeCount;
					cbThreeCount = cbSortValue[i];
					cbSortValue[i] = cbSortValue[i + 1];
					cbSortValue[i + 1] = cbThreeCount;
					bSorted = false;
				}
			}
			cbLast--;
		} while (bSorted == false);

		return;
	}
}
